/**
 * Copyright 2004 Cordys R&D B.V. 
 * 
 * This file is part of the Cordys FTP Connector. 
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 package com.eibus.applicationconnector.ftp;

import com.eibus.xml.nom.Node;
import com.eibus.soap.fault.Fault;
import com.eibus.util.logger.CordysLogger;
import com.eibus.util.logger.Severity;
import com.eibus.xml.nom.Document;
import com.eibus.applicationconnector.ftp.localization.message.Messages;

/**
 * This class is the handler for FTP's RENAME command
 */
class Rename extends FTPCommand
{
    private Document document;
	private String statusMsg;
	private String oldFileName;
	private String newFileName;
	
	private static final CordysLogger logger = CordysLogger.getCordysLogger(Rename.class);

	@Override
	public boolean prepare()
	{
		int fromNode = Node.getElementNS(requestXML, "from", getNameSpace());
		if(fromNode>0)
		{
			oldFileName = Node.getDataWithDefault(fromNode, "");
		}
		else
		{
			logger.log(Severity.ERROR, "<from> node is missing in the rename method.");
            Fault fault = response.createSOAPFault(Fault.Codes.CLIENT,Messages.RENAME_FILE);
            addFaultDetails2(fault.getDetail(),null,DEFAULT_QNAME,null,null);
            return false;
		}
		
		int toNode   = Node.getElementNS(requestXML, "to", getNameSpace());		
		if(toNode>0)
		{
			newFileName = Node.getDataWithDefault(toNode, "");
		}
		else
		{
			logger.log(Severity.ERROR, "<to> node is missing in the rename method.");
            Fault fault = response.createSOAPFault(Fault.Codes.CLIENT,Messages.RENAME_FILE);
            addFaultDetails2(fault.getDetail(),null,DEFAULT_QNAME,null,null);
            return false;
		}
		
		oldFileName = getProperFileSeparator(oldFileName, true);
		newFileName = getProperFileSeparator(newFileName, true);
		
		try
		{
			statusMsg = adapter.rename(oldFileName, newFileName);
		}
		catch(FTPException e)
		{
			logger.log(Severity.ERROR, "An error has occurred while executing rename command. " + e.getMessage());
            Fault fault = response.createSOAPFault(Fault.Codes.SERVER, Messages.RENAME_DIRECTORY_FAILURE, new String[]{e.getLocalizedMessage()});
            addFaultDetails2(fault.getDetail(), null, DEFAULT_QNAME, null, null);
            return false;
		}
		
		return true;
    }

    @Override
	public void commit()
    {
		int responseXMLNode = response.getXMLNode();
       
		document = Node.getDocument(responseXMLNode);
        
		addRequestDetailsToResponse(requestXML, responseXMLNode);
		
        int fromNode = Node.getElementNS(requestXML, "from", getNameSpace());
        int toNode = Node.getElementNS(requestXML, "to", getNameSpace());
        
        Node.createElementWithParentNS("from", Node.getDataWithDefault(fromNode, ""), responseXMLNode);
        Node.createElementWithParentNS("to", Node.getDataWithDefault(toNode, ""), responseXMLNode);
        
		if (ftpConfiguration.getProtocol().equals("SFTP"))
		{
			document.createTextElement("status", statusMsg, responseXMLNode);			
		}
		else
		{
			document.createTextElement("statuscode", statusMsg.substring(0, 3), responseXMLNode);
			document.createTextElement("status", statusMsg.substring(4), responseXMLNode);			
		}
		
		/* After the command is complete, disconnect the adapter from the FTP server */
		adapter.disconnect();
    }
}